<?php
class Warehouse_Model_ProductsBatch
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Warehouse_Model_DbTable_ProductsBatch();
	}
		
	/**
	*Create A Record  
	*@param rowSet $row Warehouse_Products_Batch's data 
	*@return boolean
	*/
	public  function create($row){
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Warehouse_Products_Batch's data 
	*@param int $ws_id 
	*@return boolean
	*/
	public  function update($row,$id){
	
		$where = $this->_table->getAdapter()->quoteInto("wpb_id= ?", $id);
        $row['wpb_last_update']=date('Y-m-d H:i:s');
		return $this->_table->update($row, $where);
		
	}
		
	/**
	*Delete One Row
	*@param int $ws_id 
	*@return boolean
	*/
	public function delete($id){
		$where = $this->_table->getAdapter()->quoteInto("wpb_id= ?", $id);	 
		return $this->_table->delete($where);
	}	

    public function getByCondition($condition=array(),$type='*',$orderby=array('wpb_id'),$pageSize=0,$page=0){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        $select->joinLeft('product', $table.'.product_id = product.product_id', 'product.product_sku');
        
		if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
            $select->where( $table.".ws_code = ?",$condition["ws_code"]);
        }
		if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where( $table.".warehouse_id = ?",$condition["warehouse_id"]);
        }
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where( $table.".product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["putaway_code"]) && $condition["putaway_code"]!=''){
            $select->where( $table.".putaway_code = ?",$condition["putaway_code"]);
        }
		if(isset($condition["receiving_code"]) && $condition["receiving_code"]!=''){
            $select->where( $table.".receiving_code = ?",$condition["receiving_code"]);
        }
		if(isset($condition["putaway_lot_number"]) && $condition["putaway_lot_number"]!=''){
            $select->where( $table.".putaway_lot_number = ?",$condition["putaway_lot_number"]);
        }
		if(isset($condition["wpb_status"]) && $condition["wpb_status"]!=''){
            $select->where( $table.".wpb_status = ?",$condition["wpb_status"]);
        }
        
        if(isset($condition["customer_sku"]) && $condition["customer_sku"]!=''){
        	$arrCon=explode('-',$condition["customer_sku"]);
        	if(count($arrCon)==2){
        		$select->where("product.customer_id = ?",$arrCon[0]);
        		$select->where("product.product_sku = ?",$arrCon[1]);
        	}
        	
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
        	$select->where("product.customer_id = ?",$condition["customer_id"]);
        }

        if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
            $select->where("product.product_sku = ?",$condition["product_sku"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}
	
	

	/**
	*Get Rowset By Warehouse_Products_Batch 
	*@param string $wpb_id 
	*@return Rowset
	*/
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto('wpb_id= ?', $id);
		return $this->_table->fetchRow($where);
	}
	
	public function getByfield($field=0,$value=1){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchRow($where);
	}
		
	public  function getByProductsBatchAndLot($condition=array()){
		$where = $this->_table->getAdapter()->quoteInto("putaway_code= ?", $condition['putaway_code']);
		$where.= $this->_table->getAdapter()->quoteInto(" and putaway_lot_number= ?", $condition['putaway_lot_number']);
		return $this->_table->fetchRow($where);
	}
	
	public function getQtyGroupByProductId($productId){
		$sql = "SELECT sum(wpb_quantity_hold) quantity_hold,sum(wpb_quantity) quantity,ws_code,product_id FROM `warehouse_products_batch` where product_id= $productId group by ws_code";
		return $this->_table->getAdapter()->fetchAll($sql);
	}	
	
	/**
	 * 获取指定条件的全部数据
	 */
	public function getAllByCond($cond){
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("1 =?",1);
        if(isset($cond["ws_code"]) && $cond["ws_code"]!=''){
            $select->where( $table.".ws_code = ?",$cond["ws_code"]);
        }
		if(isset($cond["warehouse_id"]) && $cond["warehouse_id"]!=''){
            $select->where( $table.".warehouse_id = ?",$cond["warehouse_id"]);
        }
        $sql=$select->__toString();
		return $this->_table->getAdapter()->fetchAll($sql);		
	}
	
	/**
	 *  根据多个条件更新指定数据
	 */
	public function updateByCond(array $row,array $cond)
	{
		$adapter = $this->_table->getAdapter();
		$where = $adapter->quoteInto(" ws_code= ?", $cond['ws_code']);
		$where .= $adapter->quoteInto(" and warehouse_id= ?", $cond['warehouse_id']);	
		return $this->_table->update($row, $where);
	}
	
	/**
	 * 盘点冻结库存
	 */
	public  function stocktakeHold($row,$field,$value){
		$where = $this->_table->getAdapter()->quoteInto("{$field}= ?",$value);
		return $this->_table->update($row, $where);	
	}
	
}